'''
Created on Oct 11, 2012

@author: daeheehan

Modified on Oct 31, 2012
@author: gyucheol
'''

import sys
from github import Github
from github import Issue
from github import Repository


# Github User information
GITHUB_USER_ID = "booknara"
GITHUB_USER_ID_1 = "booknaratest"
GITHUB_USER_ID_2 = "jangho87"
GITHUB_USER_ID_3 = 'luispigu'
GITHUB_USER_ID_4 = 'skswhwo'

GITHUB_USER_PW = "qwer1234"
GITHUB_USER_PW_1 = "wasd1234"

if __name__ == '__main__':
    pass

#g = Github(GITHUB_USER_ID, GITHUB_USER_PW)
g = Github(GITHUB_USER_ID_4, GITHUB_USER_PW)
print g.rate_limiting

# Github Homebrew Repository information
GITHUB_HOMEBREW_ORG_NAME = "mxcl"
GITHUB_HOMEBREW_REPOSITORY_NAME = "homebrew"

# oh-my-zsh collaborators information file
COLLABORATORS_FILE = 'homebrew_collaborators.txt'

# oh-my-zsh contributors information file
CONTRIBUTORS_FILE = 'homebrew_contributors.txt'

# oh-my-zsh collaborator followers information file
COLLABORATORS_FOLLOWER_FILE = 'homebrew_collaborators_follower.txt'

# oh-my-zsh contributors following collaborators information file
CONTRIBUTORS_FOLLOWER_COLLABORATORS_FILE = 'homebrew_follower_collaborators.txt'

project_collaborators = []
project_contributors = []
collaborators_follower = {}

contributors_follower_collaboratos = {}

try:
    # Github project collaborator list from files
    f_collaborators = open(COLLABORATORS_FILE, 'r')
    
    for line in f_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_collaborators.append(user_login)

    f_collaborators.close()

    # Github project contributors list from files
    f_contributors = open(CONTRIBUTORS_FILE, 'r')
    
    for line in f_contributors:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_contributors.append(user_login)

    f_contributors.close()

    # project_collaborators list
    # project_contributors list
    b_follower = 0
    
    #contributor_to_collaborator = dict()
    for contributor in project_contributors:
        print 'contributor : ' + contributor
#        contributor = 'drake01'
        
        for collaborator in project_collaborators:
            print 'collaborator : ' + collaborator
            if contributor == collaborator:
                print 'the same value'
                continue
            
            collabo = g.get_user(collaborator)
            
#            while True:
            follower_obj = collabo.get_followers()
            if follower_obj == None:
                b_follower = 0
                print 'None'
                break
            
            for person in follower_obj:
                #print 'follower : ' + person.login
                if person.login == contributor:
                    print 'found'
                    b_follower = 1
                    break
            
            if b_follower == 1:
                break
            
            b_follower = 0
            #contributor_to_collaborator[contributor] = b_follower
            if(g.rate_limiting[0] < 100):
                time.sleep(60 * 60)
                
        f_collaborators_follower = open(COLLABORATORS_FOLLOWER_FILE, 'a+')
        print 'Write : collaborators follower'
        sys.stdout.write(contributor + " : " + str(b_follower) + '\n')
        f_collaborators_follower.write(contributor + ", " + str(b_follower) + '\n')
        f_collaborators_follower.close()
        

except Exception as e: 
    print 'The final Exception', e.args[0]

print
print 'Finally the end'
